library(tidyverse)
library(redist)

source("R/00_custom_functions.R")


nc_shp <- read_rds("data/NC/nc_shp.rds")
nc_adj <- read_rds("data/NC/nc_adj.Rds")

# set up for redist ----
nc_map_orig <- redist_map(nc_shp, existing_plan = cd_17, pop_tol = 0.01, adj = nc_adj, total_pop = pop)
nc_map_da04 <- redist_map(nc_shp, existing_plan = cd_17, pop_tol = 0.01, adj = nc_adj, total_pop = v4_pop)
nc_map_da12 <- redist_map(nc_shp, existing_plan = cd_17, pop_tol = 0.01, adj = nc_adj, total_pop = v12_pop)
nc_map_da19 <- redist_map(nc_shp, existing_plan = cd_17, pop_tol = 0.01, adj = nc_adj, total_pop = v19_pop)

# Run SMC -------
fs::dir_create("data/NC/sim/CD") # create container
sim_CD <- function(data, .nsims = 2e5) {
    redist_smc(data,
               nsims = .nsims,
               counties = County,
               verbose = TRUE)
}


plans_da19 <- sim_CD(nc_map_da19)
write_rds(plans_da19, "data/NC/sim/CD/plans_da19.rds", compress = "xz")

plans_da12 <- sim_CD(nc_map_da12)
write_rds(plans_da12, "data/NC/sim/CD/plans_da12.rds", compress = "xz")

plans_orig <- sim_CD(nc_map_orig)
write_rds(plans_orig, "data/NC/sim/CD/plans_orig.rds", compress = "xz")

plans_da04 <- sim_CD(nc_map_da04)
write_rds(plans_da04, "data/NC/sim/CD/plans_da04.rds", compress = "xz")


# save map
write_rds(nc_map_orig, "data/NC/sim/CD/redist_map_orig.rds")


